<?php

namespace App\Admin\Controllers;

use App\Models\Resume;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ResumeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '简历';

    private function __($key)
    {
        return [
            'Id' => 'ID',
            'Name' => '姓名',
            'Avatar' => '头像',
            'UserName' => '会员',
            'Birth' => '生日',
            'Record' => '学历',
            'Signature' => '座右铭',
            'Job year' => '工作年限',
            'Sex' => '性别',
            'Position' => '求职职位',
            'School' => '毕业学校',
            'Experience' => '工作经验',
            'Images' => '证书',
            'Created at' => '创建时间',
            'Updated at' => '修改时间',
        ][$key] ?? $key;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Resume());

        $grid->disableCreateButton();
        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableColumnSelector();

        $grid->actions(function ($actions) {

            // 去掉删除
            $actions->disableDelete();

            // 去掉编辑
            $actions->disableEdit();
        });

        $grid->column('id', $this->__('Id'));
        $grid->column('name', $this->__('Name'))->filter('like');
        $grid->column('avatar', $this->__('Avatar'))->image('/storage', 50, 50);
        $grid->column('user_id', $this->__('UserName'))->display(function($userId) {
            return User::where('id', $userId)->value('nickname') ?: '--';
        });
        // $grid->column('data', $this->__('Data'));
        $grid->column('birth', $this->__('Birth'))->sortable();
        $grid->column('record', $this->__('Record'))->filter('like');
        $grid->column('signature', $this->__('Signature'))->filter('like');
        $grid->column('job_year', $this->__('Job year'))->filter('range')->sortable();
        $grid->column('sex', $this->__('Sex'))->using([1 => '女', 0 => '男'])->filter([1 => '女', 0 => '男']);
        $grid->column('position', $this->__('Position'))->filter('like');
        $grid->column('school', $this->__('School'))->filter('like');
        // $grid->column('experience', $this->__('Experience'));
        $grid->column('images', $this->__('Images'))->carousel(80, 80, '/storage');
        $grid->column('created_at', $this->__('Created at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        })->sortable();
        $grid->column('updated_at', $this->__('Updated at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        })->sortable();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Resume::findOrFail($id));

        $show->panel()
        ->tools(function ($tools) {
            $tools->disableEdit();
            $tools->disableDelete();
        });

        $show->field('id', $this->__('Id'));
        $show->field('name', $this->__('Name'));
        $show->field('avatar', $this->__('Avatar'))->image('/storage/', 100, 100);;
        $show->field('user_id',  $this->__('UserName'))->as(function($userId) {
            return User::where('id', $userId)->value('nickname') ?: '--';
        });
        $show->field('birth', $this->__('Birth'));
        $show->field('record', $this->__('Record'));
        $show->field('signature', $this->__('Signature'));
        $show->field('job_year', $this->__('Job year'));
        $show->field('sex', $this->__('Sex'))->using([1 => '女', 0 => '男']);
        $show->field('position', $this->__('Position'));
        $show->field('school', $this->__('School'));
        $show->field('experience', $this->__('Experience'));
        $show->field('images', $this->__('Images'))->carousel(100, 100, '/storage/');

        $show->field('created_at', $this->__('Created at'))->as(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        });
        $show->field('updated_at', $this->__('Updated at'))->as(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Resume());

        $form->text('data', $this->__('Data'));
        $form->image('avatar', $this->__('Avatar'));
        $form->datetime('birth', $this->__('Birth'))->default(date('Y-m-d H:i:s'));
        $form->text('record', $this->__('Record'));
        $form->text('signature', $this->__('Signature'));
        $form->number('job_year', $this->__('Job year'));
        $form->switch('sex', $this->__('Sex'));
        $form->text('position', $this->__('Position'));
        $form->text('school', $this->__('School'));
        $form->textarea('experience', $this->__('Experience'));
        $form->text('images', $this->__('Images'));
        $form->number('user_id', $this->__('User id'));

        return $form;
    }
}
